package com.zsr.example.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsr.example.config.annotation.Logical;
import com.zsr.example.config.annotation.OptLog;
import com.zsr.example.config.annotation.RequiresPermissions;
import com.zsr.example.entity.EduTeacher;
import com.zsr.example.service.impl.EduTeacherServiceImpl;
import com.zsr.example.util.CommonUtil;
import com.zsr.example.vo.TeacherReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zsr.example.util.constants.SysLogConstants.*;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author zsr
 * @since 2021-10-15
 */
@RestController
@RequestMapping("/teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherServiceImpl teacherService;

    /**
     * 查询列表
     * 课程列表需要加载教师下拉列表,权限逻辑为or
     */
    @RequiresPermissions(value = {"teacher:list","course:list"},logical = Logical.OR)
    @GetMapping("/findAll")
    public JSONObject listArticle() {
        QueryWrapper<EduTeacher> queryWrapper=new QueryWrapper<>();
        List<EduTeacher> list = teacherService.list(queryWrapper);
        return CommonUtil.successJson(list);
    }

    /**
     * 分页查询教师列表[未用]
     *
     * @param current
     * @param limit
     * @return
     */
    @RequiresPermissions("teacher:list")
    @GetMapping("pageTeacher/{current}/{limit}")
    public JSONObject pageListTeacher(@PathVariable long current,
                                      @PathVariable long limit) {
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);

        //调用方法实现分页
        //调用方法时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        teacherService.page(pageTeacher, null);

        long total = pageTeacher.getTotal();//总记录数
        List<EduTeacher> records = pageTeacher.getRecords(); //数据list集合

        return CommonUtil.successPage(total, records);
    }

    /**
     * 删除教师
     * @param id
     * @return
     */
    @OptLog(optType = DELETE,optDesc = "删除教师")
    @RequiresPermissions("teacher:delete")
    @DeleteMapping("{id}")
    public JSONObject removeTeacher(
            @PathVariable String id) {
        boolean flag = teacherService.removeById(id);
        return flag ? CommonUtil.successJson() : CommonUtil.errorJson();
    }

    /**
     * 带条件查询教师列表
     * @param current
     * @param limit
     * @param teacherReq
     * @return
     */
    //@BusinessLog(description = "带条件查询教师列表")
    @RequiresPermissions("teacher:list")
    @PostMapping("pageListTeacherCondition/{current}/{limit}")
    public JSONObject pageListTeacherCondition(
            @PathVariable long current,
            @PathVariable long limit,
            @RequestBody(required = false) TeacherReq teacherReq) {//@RequestBody
        // 使用json封装到参数,需要post请求
        //创建page对象
        Page<EduTeacher> page = new Page<>(current, limit);

        teacherService.pageQuery(page, teacherReq);


        long total = page.getTotal();//总记录数
        List<EduTeacher> records = page.getRecords(); //数据list集合

        return CommonUtil.successPage(total, records);
    }

    /**
     * 添加教师
     * @param eduTeacher
     * @return
     */
    @RequiresPermissions("teacher:add")
    @PostMapping("addTeacher")
    public JSONObject addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = teacherService.save(eduTeacher);
        return save? CommonUtil.successJson():CommonUtil.errorJson();
    }

    /**
     * 获取教师信息 [未用]
     * @param id
     * @return
     */
    @RequiresPermissions("teacher:list")
    @GetMapping("getTeacher/{id}")
    public JSONObject getTeacher(@PathVariable String id){
        EduTeacher teacher = teacherService.getById(id);
        return teacher==null? CommonUtil.errorJson():CommonUtil.successJson(teacher);
    }

    /**
     * 更新教师信息
     * @param eduTeacher
     * @return
     */
    @OptLog(optType = UPDATE,optDesc = "更新教师")
    @PostMapping("updateTeacher")
    public JSONObject updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean b = teacherService.updateById(eduTeacher);
        return b ? CommonUtil.successJson() : CommonUtil.errorJson();
    }
}

